﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SMS
{
    public partial class uDanhsachxe : UserControl
    {
        fLogin main;
        public uDanhsachxe(fLogin main1)
        {
            InitializeComponent();
            main = main1;
        }


        public void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            main.Capnhatthongtinxe.ShowDialog();
        }


        private void bntTonkho_Click(object sender, EventArgs e)
        {
            main.Tonkho.ShowDialog();
        }



        private DataTable show()
        {
            // show sau khi chạy
            SqlDataAdapter danhsachxe;
            DataTable dt = new DataTable();
            //KetNoiDataBase db = new KetNoiDataBase();
            SqlConnection con = main.KetNoiDB.GetConServer();
            //da = new SqlDataAdapter("SELECT * FROM NGUOIDUNG ", con);
            danhsachxe = new SqlDataAdapter("SELECT XEMAY.SOKHUNG as 'Số khung',TENLOAIXE as 'Tên Loại Xe',TENMAUXE as 'Tên Màu Xe',SOMAY as 'Số máy',SOBH as 'Sổ bảo hành',CTXEMAY.GIA as 'Giá',TINHTRANGBD as 'Tình trạng' , NGAYMUA as 'Ngày mua', CMND as 'CMND khách hàng' FROM XEMAY ,CTXEMAY , MAUXE , LOAIXE  WHERE XEMAY.SOKHUNG= CTXEMAY.SOKHUNG and CTXEMAY.MAQTLOAIXE = LOAIXE.MAQTLOAIXE and CTXEMAY.MAMAUXE = MAUXE.MAMAUXE ", con);
            dt.Clear();
            danhsachxe.Fill(dt); // do ket qua truy van vao ban
            return dt;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e){

            int selectRow = dataGridView1.CurrentRow.Index;
            string dataSoKhung = dataGridView1.Rows[selectRow].Cells[0].Value.ToString();
            string dataTenLoaiXe = dataGridView1.Rows[selectRow].Cells[1].Value.ToString();
            string dataMauXe = dataGridView1.Rows[selectRow].Cells[2].Value.ToString();
            string dataSoMay = dataGridView1.Rows[selectRow].Cells[3].Value.ToString();
            string dataSobh = dataGridView1.Rows[selectRow].Cells[4].Value.ToString();
            string dataGia = dataGridView1.Rows[selectRow].Cells[5].Value.ToString();
            string dataTinhTrang = dataGridView1.Rows[selectRow].Cells[6].Value.ToString();
            main.Capnhatthongtinxe.txtSoKhung.Text = dataSoKhung;

            main.Capnhatthongtinxe.txtSoMay.Text = dataSoMay;
            main.Capnhatthongtinxe.txtLoaiXeDung.Text = dataTenLoaiXe;
            main.Capnhatthongtinxe.txtMauXeDung.Text = dataMauXe; 

            main.Capnhatthongtinxe.txtSobh.Text = dataSobh;
            main.Capnhatthongtinxe.txtGia.Text = dataGia;
            main.Capnhatthongtinxe.txtTinhTrang.Text = dataTinhTrang;
            main.Capnhatthongtinxe.ShowDialog();


        }

        public void uDanhsachxe_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = show();
        }

        public void updateDSX()
        {
            dataGridView1.DataSource = show();
        }
    }
}
